package com.tiyende.credit.loop.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.common.CommonDialog
import com.tiyende.credit.loop.databinding.TiyendeDialogVersionBinding
import com.tiyende.credit.loop.databinding.TiyendeDialogVersionInfoItemBinding
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.utils.TiyendeDensityUtil
import com.tiyende.credit.loop.utils.helper.TiyendeConfigsHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：版本更新dialog
 */
class TiyendeVersionDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogVersionBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initTiyendeWindow() {
        val params = window?.attributes
        params?.width = TiyendeApp.app.getPhoneWidth() - TiyendeDensityUtil.dp2px(48f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogVersionBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initTiyendeView() {
        val versionData = TiyendeConfigsHelper.queryTiyendeVersionObject()
        versionData?.let {
            binding.updateVersion.text = "V${it.optString("versionName")}"
            //添加更新描述
            var versionRemark = it.optString("versionRemark")
            //高棉语
            if (TiyendeSharedHelper.queryTiyendeSharedString(TiyendeSharedKey.TIYENDE_LANGUAGE_TAGS) == "km"){
                if (it.optString("versionRemark_km","").isNotEmpty()){
                    versionRemark = it.optString("versionRemark_km")
                }
            }
            addTiyendeUpdateInfoItem(versionRemark)

            //是否强制更新
            val isMustUpdate = TiyendeConfigsHelper.isTiyendeUpdateMust()
            if (isMustUpdate){
                binding.updateWithin.visibility = View.GONE
                binding.updateClose.visibility = View.GONE
                setCancelable(false)
                setCanceledOnTouchOutside(false)
            } else {
                binding.updateWithin.visibility = View.VISIBLE
                binding.updateClose.visibility = View.VISIBLE
                setCancelable(true)
                setCanceledOnTouchOutside(true)
            }
        }

        val showKey = "30"
        val showText = String.format(context.getString(R.string.tiyende_update_within),showKey)
        val index = showText.indexOf(showKey)
        val stringSpannable = SpannableString(showText)
        stringSpannable.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF4E27")), index, index + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //加粗
        stringSpannable.setSpan(StyleSpan(Typeface.BOLD), index, index + showKey.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.updateWithin.text = stringSpannable
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.updateClose)//关闭
        setOnClickListener(binding.updateNow) //立即更新
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.updateClose -> {
                addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_UPDATE_CLOSE,eventRemark)
            }
            R.id.updateNow -> {
                addEventPointRequestAction(TiyendeEventPointType.CLICK.TIYENDE_CLICK_UPDATE_NOW,eventRemark)
                mCallback?.onClick(v)
            }
        }
    }

    /**
     * 添加更新描述
     */
    private fun addTiyendeUpdateInfoItem(remark: String){
        binding.updateInfoLayout.removeAllViews()
        if (!remark.contains("$")){
            val itemBinding = TiyendeDialogVersionInfoItemBinding.inflate(layoutInflater)
            itemBinding.textUpdateInfo.text = remark
            binding.updateInfoLayout.addView(itemBinding.root)
            return
        }
        val list = remark.split("$")
        list.forEach {
            val itemBinding = TiyendeDialogVersionInfoItemBinding.inflate(layoutInflater)
            itemBinding.textUpdateInfo.text = it
            binding.updateInfoLayout.addView(itemBinding.root)
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_UPDATE_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_UPDATE_DISMISS,eventRemark)
    }
}